package com.meituan.catering.management.order.biz.model.converter;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.meituan.catering.management.order.dao.model.CateringOrderItemDO;

import java.util.List;
import java.util.Map;

/**
 * @author Believe
 * Date 2022/6/7 14:19
 */
public class AdjustCateringItemFilter {
    public static List<CateringOrderItemDO> itemDOUpdateFilterList(List<CateringOrderItemDO> cateringOrderItemDOList, List<CateringOrderItemDO> queryItemList) {
        List<CateringOrderItemDO> orderItemDOList = Lists.newLinkedList();
        Map<Long, List<CateringOrderItemDO>> listMap = Maps.newHashMap();
        for (CateringOrderItemDO orderItemDO : queryItemList) {
            if (listMap.containsKey(orderItemDO.getProductId())) {
                List<CateringOrderItemDO> cateringOrderItemDOS = listMap.get(orderItemDO.getProductId());
                cateringOrderItemDOS.add(orderItemDO);
            } else {
                List<CateringOrderItemDO> cateringOrderItemDOS = Lists.newLinkedList();
                cateringOrderItemDOS.add(orderItemDO);
                listMap.put(orderItemDO.getProductId(), cateringOrderItemDOS);
            }
        }
        for (CateringOrderItemDO orderItemDO : cateringOrderItemDOList) {
            if (listMap.containsKey(orderItemDO.getProductId())) {
                orderItemDOList.add(orderItemDO);
            }
        }
        return orderItemDOList;
    }

    public static List<CateringOrderItemDO> itemDOInsertFilterList(List<CateringOrderItemDO> cateringOrderItemDOList, List<CateringOrderItemDO> queryItemList) {
        List<CateringOrderItemDO> orderItemDOList = Lists.newLinkedList();
        Map<Long, List<CateringOrderItemDO>> listMap = Maps.newHashMap();
        for (CateringOrderItemDO orderItemDO : queryItemList) {
            if (listMap.containsKey(orderItemDO.getProductId())) {
                List<CateringOrderItemDO> cateringOrderItemDOS = listMap.get(orderItemDO.getProductId());
                cateringOrderItemDOS.add(orderItemDO);
            } else {
                List<CateringOrderItemDO> cateringOrderItemDOS = Lists.newLinkedList();
                cateringOrderItemDOS.add(orderItemDO);
                listMap.put(orderItemDO.getProductId(), cateringOrderItemDOS);
            }
        }
        for (CateringOrderItemDO orderItemDO : cateringOrderItemDOList) {
            if (!listMap.containsKey(orderItemDO.getProductId())) {
                orderItemDOList.add(orderItemDO);
            }
        }
        return orderItemDOList;
    }
}
